package com.dayuanit.dy14.atm.demo2.dy14atmweb.controller;

import com.dayuanit.dy14.atm.demo2.dy14atmcommon.dto.ResponseDTO;
import com.dayuanit.dy14.atm.demo2.dy14atmcommon.vo.LoginVO;
import com.dayuanit.dy14.atm.demo2.dy14atmcommon.vo.RegistVO;
import com.dayuanit.dy14.atm.demo2.dy14atmdao.entity.User;
import com.dayuanit.dy14.atm.demo2.dy14atmservice.service.UserService;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@RestController
public class UserController extends BaseController {

    @Autowired
    private UserService userService;

    @RequestMapping(value = "/user/regist", method = RequestMethod.POST)
    public ResponseDTO regist(@Validated RegistVO registVO) {
        userService.regist(registVO);
        return ResponseDTO.success();
    }

    @RequestMapping(value = "/user/login2", method = RequestMethod.POST)
    public ResponseDTO login2(LoginVO loginVO) {
        User user = userService.login(loginVO);
        session.setAttribute("loginUser", user);
        return ResponseDTO.success();
    }

    @RequestMapping(value = "/user/logout", method = RequestMethod.POST)
    public ResponseDTO logout() {
        session.removeAttribute("loginUser");
        session.invalidate();
        return ResponseDTO.success();
    }

    @RequestMapping(value = "/user/login", method = RequestMethod.POST)
    public ResponseDTO login(String username, String pwd, String pwdSign, HttpServletRequest request, HttpServletResponse response) {
//        if (StringUtils.isNoneBlank(pwdSign)) {
//            String sign = DigestUtils.md5Hex(username + md5Salt + "2020-12-31");
//            if (!sign.equals(pwdSign)) {
//                return ResponseDTO.fail("刷新页面重新登录");
//            } else{
//                User user = userService.loadUser(username);
//                request.getSession().setAttribute("loginUser", user);
//                return ResponseDTO.success();
//            }
//        }

        HttpSession session = request.getSession();//创建session

        System.out.println("sessionId=" + session.getId());

        session.setAttribute("username", username);

        String mingwen = username + md5Salt + 72;
        Cookie cookie = new Cookie("username", username);
        cookie.setHttpOnly(true);
        cookie.setPath("/");

        Cookie cookie2 = new Cookie("pwdSign", DigestUtils.md5Hex(username + md5Salt + "72"));
        cookie2.setHttpOnly(true);
        cookie2.setPath("/");
        cookie.setMaxAge(-1);

        Cookie cookie3 = new Cookie("pwdSignExpire", "72");
        cookie2.setHttpOnly(true);
        cookie2.setPath("/");
        cookie.setMaxAge(-1);

        response.addCookie(cookie);
        response.addCookie(cookie2);
        response.addCookie(cookie3);

        //TODO 记住密码

        return ResponseDTO.success();
    }

    @RequestMapping(value = "/user/testSession", method = RequestMethod.GET)
    public ResponseDTO login(HttpServletRequest request) {


        System.out.println(">>>>sessionId=" + request.getSession().getId());
        Cookie[] cookies = request.getCookies();
        if (null != cookies) {
            for(Cookie cookie : cookies) {
                System.out.println(cookie.getName() + "=" + cookie.getValue());
            }
        }

        return ResponseDTO.success();
    }

}
